package com.crm.set.controller;

import com.crm.common.pojo.User;
import com.crm.global.exception.ExceptionEnum;
import com.crm.global.result.Message;
import com.crm.set.service.UserService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
@Api(tags = "用户管理")
public class UserController {
    @Autowired
    private UserService userService;

    @ApiOperation(value = "查询所有的用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "页码",paramType = "query",dataType = "int",required = true),
            @ApiImplicitParam(name = "size",value = "页面展示数据条数",paramType = "query",dataType = "int",required = true)
    })
    @GetMapping("/getAll")
    public ResponseEntity<Message<PageInfo<User>>> getAll(int page, int size) {
        return ResponseEntity.ok(new Message<>(ExceptionEnum.SUCCESS,userService.listAll(page,size)));
    }

    @ApiOperation(value = "新增用户")
    @PostMapping("/save")
    public ResponseEntity<Message> save(User user) {
        userService.saveOrUpdate(user);
        return ResponseEntity.ok(new Message(ExceptionEnum.SUCCESS));
    }

    @ApiOperation(value = "更新用户")
    @PostMapping("/update")
    public ResponseEntity<Message> update(User user) {
        userService.saveOrUpdate(user);
        return ResponseEntity.ok(new Message(ExceptionEnum.SUCCESS));
    }

    @ApiOperation(value = "删除用户")
    @GetMapping("/delete")
    @ApiImplicitParam(name = "id",value = "用户编号",required = true)
    public ResponseEntity<Message> delete(long id) {
        userService.delete(id);
        return ResponseEntity.ok(new Message(ExceptionEnum.SUCCESS));
    }
}